/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.Formula;

import br.com.ConexaoBanco.ConexaoMySQL;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.rules.*;
import weka.classifiers.functions.*;
import weka.classifiers.meta.*;
import weka.core.Instances;

/**
 *
 * @author User
 */
public class ARFFpesquisa extends javax.swing.JInternalFrame {
    File arquivoTreinamento, arquivoTeste;
    FileReader leitorTreinamento, leitorTeste;
    Instances entradasTreinamento, entradasTeste;
    Classifier classificador;
    Evaluation avaliador;
    boolean podeTestar;
    /**
     * Creates new form ARFFpesquisa
     */
    public ARFFpesquisa() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgrRadios = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        lblPiloto = new javax.swing.JLabel();
        jcbPiloto = new javax.swing.JComboBox();
        lblParada = new javax.swing.JLabel();
        jcbParada = new javax.swing.JComboBox();
        lblPneu = new javax.swing.JLabel();
        jcbPneu = new javax.swing.JComboBox();
        lblVoltas = new javax.swing.JLabel();
        lblComb = new javax.swing.JLabel();
        jcbVoltas = new javax.swing.JComboBox();
        jcbComb = new javax.swing.JComboBox();
        lblPesquisa = new javax.swing.JLabel();
        jrbPiloto = new javax.swing.JRadioButton();
        jrbParada = new javax.swing.JRadioButton();
        jrbPneu = new javax.swing.JRadioButton();
        jrbVoltas = new javax.swing.JRadioButton();
        jrbComb = new javax.swing.JRadioButton();
        btnExport = new javax.swing.JButton();
        jFileChooser1 = new javax.swing.JFileChooser();
        btnWEKA = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Exportar Arquivo ARFF de Pesquisa");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/icone/16 x 16/import.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                ARFFpeOpened(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));

        lblPiloto.setText("Piloto:");

        lblParada.setText("Parada:");

        lblPneu.setText("Pneu:");

        jcbPneu.setToolTipText("");

        lblVoltas.setText("Qtd. Voltas:");

        lblComb.setText("Qtd. Comustível:");

        jcbVoltas.setToolTipText("");

        jcbComb.setToolTipText("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPiloto)
                            .addComponent(lblParada)
                            .addComponent(lblPneu))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbParada, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbPneu, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblVoltas)
                            .addComponent(lblComb))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jcbComb, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbVoltas, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPiloto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbParada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblParada))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbPneu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPneu))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblVoltas)
                    .addComponent(jcbVoltas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblComb)
                    .addComponent(jcbComb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        lblPesquisa.setText("Pesquisar por:");

        bgrRadios.add(jrbPiloto);
        jrbPiloto.setText("Piloto");
        jrbPiloto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbPilitoActionPerformed(evt);
            }
        });

        bgrRadios.add(jrbParada);
        jrbParada.setText("Parada");
        jrbParada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbParadaActionPerformed(evt);
            }
        });

        bgrRadios.add(jrbPneu);
        jrbPneu.setText("Pneu");
        jrbPneu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbPneuActionPerformed(evt);
            }
        });

        bgrRadios.add(jrbVoltas);
        jrbVoltas.setText("Qtd. Voltas");
        jrbVoltas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbVoltasActionPerformed(evt);
            }
        });

        bgrRadios.add(jrbComb);
        jrbComb.setText("Combustível");
        jrbComb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbCombActionPerformed(evt);
            }
        });

        btnExport.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icone/compfile.png"))); // NOI18N
        btnExport.setText("Exportar");
        btnExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportActionPerformed(evt);
            }
        });

        jFileChooser1.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
        jFileChooser1.setDialogTitle("");
        jFileChooser1.setName("");

        btnWEKA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icone/quiz.png"))); // NOI18N
        btnWEKA.setLabel("Pesquisar WEKA");
        btnWEKA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWEKAActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnExport, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnWEKA, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jrbPiloto)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jrbParada)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jrbPneu)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jrbVoltas)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jrbComb))
                            .addComponent(lblPesquisa))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblPesquisa)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jrbPiloto)
                    .addComponent(jrbParada)
                    .addComponent(jrbPneu)
                    .addComponent(jrbVoltas)
                    .addComponent(jrbComb))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExport)
                    .addComponent(btnWEKA))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jrbPilitoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbPilitoActionPerformed
        // TODO add your handling code here:
        if (jrbPiloto.isSelected()) {
            jPanel1.setVisible(true);
            jcbPiloto.setEnabled(false);
            jcbParada.setEnabled(true);
            jcbPneu.setEnabled(true);
            jcbVoltas.setEnabled(true);
            jcbComb.setEnabled(true);
        }
    }//GEN-LAST:event_jrbPilitoActionPerformed

    private void jrbParadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbParadaActionPerformed
        // TODO add your handling code here:
        if (jrbParada.isSelected()) {
            jPanel1.setVisible(true);
            jcbPiloto.setEnabled(true);
            jcbParada.setEnabled(false);
            jcbPneu.setEnabled(true);
            jcbVoltas.setEnabled(true);
            jcbComb.setEnabled(true);
        }
    }//GEN-LAST:event_jrbParadaActionPerformed

    private void jrbPneuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbPneuActionPerformed
        // TODO add your handling code here:
        if (jrbPneu.isSelected()) {
            jPanel1.setVisible(true);
            jcbPiloto.setEnabled(true);
            jcbParada.setEnabled(true);
            jcbPneu.setEnabled(false);
            jcbVoltas.setEnabled(true);
            jcbComb.setEnabled(true);
        }
    }//GEN-LAST:event_jrbPneuActionPerformed

    private void jrbVoltasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbVoltasActionPerformed
        // TODO add your handling code here:
        if (jrbVoltas.isSelected()) {
            jPanel1.setVisible(true);
            jcbPiloto.setEnabled(true);
            jcbParada.setEnabled(true);
            jcbPneu.setEnabled(true);
            jcbVoltas.setEnabled(false);
            jcbComb.setEnabled(true);
        }
    }//GEN-LAST:event_jrbVoltasActionPerformed

    private void jrbCombActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbCombActionPerformed
        // TODO add your handling code here:
        if (jrbComb.isSelected()) {
            jPanel1.setVisible(true);
            jcbPiloto.setEnabled(true);
            jcbParada.setEnabled(true);
            jcbPneu.setEnabled(true);
            jcbVoltas.setEnabled(true);
            jcbComb.setEnabled(false);
        }
    }//GEN-LAST:event_jrbCombActionPerformed

    private void ARFFpeOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_ARFFpeOpened
        ResultSet rs;
        try {
            Connection com = ConexaoMySQL.getConexaoMySQL();
            Statement stm = com.createStatement();
            rs = stm.executeQuery("SELECT * FROM piloto");
            rs.first();
            while (!rs.isAfterLast()) {
                ComboItem combo = new ComboItem(rs.getString("id_piloto"), rs.getString("nom_piloto"));
                jcbPiloto.addItem(combo);
                rs.next();
            }
            rs = stm.executeQuery("SELECT * FROM tipoparada");
            rs.first();
            while (!rs.isAfterLast()) {
                ComboItem combo = new ComboItem(rs.getString("id_tipo"), rs.getString("nom_tipo"));
                jcbParada.addItem(combo);
                rs.next();
            }
            String pneu;
            rs = stm.executeQuery("SELECT * FROM pneu");
            rs.first();
            while (!rs.isAfterLast()) {
                pneu = rs.getString("marca_pneu") + " | " + rs.getString("tipo_pneu");
                ComboItem combo = new ComboItem(rs.getString("id_pneu"), pneu);
                jcbPneu.addItem(combo);
                rs.next();
            }
            rs = stm.executeQuery("SELECT DISTINCT num_volta FROM cadparada ORDER BY num_volta ASC");
            rs.first();
            while (!rs.isAfterLast()) {
                jcbVoltas.addItem(rs.getString("num_volta"));
                rs.next();
            }
            rs = stm.executeQuery("SELECT DISTINCT qtd_comb FROM cadparada ORDER BY qtd_comb ASC");
            rs.first();
            while (!rs.isAfterLast()) {
                jcbComb.addItem(rs.getString("qtd_comb"));
                rs.next();
            }
            //jrbPiloto.setSelected(true);
            jPanel1.setVisible(false);
        } catch (SQLException ex) {
            Logger.getLogger(CadastrarParada.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ARFFpeOpened

    private String criaAtributos(String nomeAtributo, String nomeTabela, String nomeCampo) {
        String atrib = "@attribute " + nomeAtributo + " {";
        ResultSet rs;
        try {
            Connection com = ConexaoMySQL.getConexaoMySQL();
            Statement stm = com.createStatement();
            rs = stm.executeQuery("SELECT DISTINCT " + nomeCampo + " FROM " + nomeTabela + " ORDER BY " + nomeCampo + " ASC");
            rs.first();
            while (!rs.isAfterLast()) {
                atrib = atrib + rs.getString(nomeCampo).replace(" ", "_") + ", ";
                rs.next();
            }
            //Apaga o ulitmo espaço e a ulitma virgula adicionados no while
            atrib = atrib.substring(0, atrib.length() - 2);
            atrib = atrib + "}";
        } catch (SQLException ex) {
            Logger.getLogger(CadastrarPiloto.class.getName()).log(Level.SEVERE, null, ex);
        }
        return atrib;

    }
    
    private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportActionPerformed
        //recebe se o usuario clicou no botao salvar ou cancelar
        ComboItem combo;
        jFileChooser1.setSelectedFile(new File("formula1_pesquisa.arff"));
        int salvar = jFileChooser1.showSaveDialog(this);
        File arq;
        //testa se ele escoulheu salvar
        if (salvar == JFileChooser.APPROVE_OPTION) {
            arq = jFileChooser1.getSelectedFile();
            String atributo, dados;

            try {
                //tenta criar o arquivo
                arq.createNewFile();
                FileWriter fileWriter = new FileWriter(arq, false);

                PrintWriter printWriter = new PrintWriter(fileWriter);

                printWriter.println("@RELATION formula1");
                printWriter.println("");

                //Pesquisar o atributo piloto
                atributo = criaAtributos("piloto", "piloto", "nom_piloto");
                printWriter.println(atributo);

                //Pesquisar o atributo parada
                atributo = criaAtributos("parada", "tipoparada", "nom_tipo");
                printWriter.println(atributo);

                //Pesquisar o atributo pneu
                atributo = criaAtributos("pneu", "pneu", "tipo_pneu");
                printWriter.println(atributo);

                //Pesquisar o atributo pneu
                atributo = criaAtributos("voltas", "cadparada", "num_volta");
                printWriter.println(atributo);

                //Pesquisar o atributo pneu
                atributo = criaAtributos("combustivel", "cadparada", "qtd_comb");
                printWriter.println(atributo);

                printWriter.println("");
                printWriter.println("@data");
                
                dados = "";
                
                if (jrbPiloto.isSelected()){
                    combo = (ComboItem) jcbParada.getSelectedItem();
                    dados = "?," + combo.getLabel().replace(" ", "_");
                    combo = (ComboItem) jcbPneu.getSelectedItem();
                    dados = dados + "," + combo.getLabel().substring((combo.getLabel().indexOf("|")) + 2).replace(" ", "_");
                    //combo = (ComboItem) jcbVoltas.getSelectedItem();
                    dados = dados + "," + jcbVoltas.getSelectedItem().toString().replace(" ", "_");
                    //combo = (ComboItem) jcbComb.getSelectedItem();
                    dados = dados + "," + jcbComb.getSelectedItem().toString().replace(" ", "_");
                }
                
                else if (jrbParada.isSelected()){
                    combo = (ComboItem) jcbPiloto.getSelectedItem();
                    dados = combo.getLabel().replace(" ", "_") + ",?";
                    combo = (ComboItem) jcbPneu.getSelectedItem();
                    dados = dados + "," + combo.getLabel().substring((combo.getLabel().indexOf("|")) + 2).replace(" ", "_");
                    //combo = (ComboItem) jcbParada.getSelectedItem();
                    //combo = (ComboItem) jcbVoltas.getSelectedItem();
                    dados = dados + "," + jcbVoltas.getSelectedItem().toString().replace(" ", "_");
                    //combo = (ComboItem) jcbComb.getSelectedItem();
                    dados = dados + "," + jcbComb.getSelectedItem().toString().replace(" ", "_");
                }
                
                else if (jrbPneu.isSelected()){
                    combo = (ComboItem) jcbPiloto.getSelectedItem();
                    dados = combo.getLabel().replace(" ", "_");
                    combo = (ComboItem) jcbParada.getSelectedItem();
                    dados = dados + "," + combo.getLabel().replace(" ", "_");
                    //combo = (ComboItem) jcbVoltas.getSelectedItem();
                    dados = dados + ",?," + jcbVoltas.getSelectedItem().toString().replace(" ", "_");
                    //combo = (ComboItem) jcbComb.getSelectedItem();
                    dados = dados + "," + jcbComb.getSelectedItem().toString().replace(" ", "_");
                }
                
                else if (jrbVoltas.isSelected()){
                    combo = (ComboItem) jcbPiloto.getSelectedItem();
                    dados = combo.getLabel().replace(" ", "_");
                    combo = (ComboItem) jcbParada.getSelectedItem();
                    dados = dados + "," + combo.getLabel().replace(" ", "_");
                    combo = (ComboItem) jcbPneu.getSelectedItem();
                    dados = dados + "," + combo.getLabel().substring((combo.getLabel().indexOf("|")) + 2).replace(" ", "_");
                    //combo = (ComboItem) jcbComb.getSelectedItem();
                    dados = dados + ",?," + jcbComb.getSelectedItem().toString().replace(" ", "_");
                }
                
                else if (jrbComb.isSelected()){
                    combo = (ComboItem) jcbPiloto.getSelectedItem();
                    dados = combo.getLabel().replace(" ", "_");
                    combo = (ComboItem) jcbParada.getSelectedItem();
                    dados = dados + "," + combo.getLabel().replace(" ", "_");
                    combo = (ComboItem) jcbPneu.getSelectedItem();
                    dados = dados + "," + combo.getLabel().substring((combo.getLabel().indexOf("|")) + 2).replace(" ", "_");
                    //combo = (ComboItem) jcbVoltas.getSelectedItem();
                    dados = dados + "," + jcbVoltas.getSelectedItem().toString().replace(" ", "_");
                    dados = dados + ",?";
                }
                
                printWriter.println(dados);
                
                //libera a escrita
                printWriter.flush();
                //Fecha o arquivo
                printWriter.close();
                JOptionPane.showMessageDialog(null, "Arquivo exportado com sucesso!",
                        "Arquivo exportado com sucesso!", 1);
                
                 } catch (IOException ex) {
                Logger.getLogger(ExportarARFF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btnExportActionPerformed

    private void btnWEKAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWEKAActionPerformed
        // TODO add your handling code here:
        try {
            arquivoTreinamento = new File("/users/user/desktop/formula1.arff");
            leitorTreinamento = new FileReader(arquivoTreinamento);
            entradasTreinamento = new Instances(leitorTreinamento);
            entradasTreinamento.setClassIndex(4);
            classificador = new NaiveBayes();
            classificador.buildClassifier(entradasTreinamento);
            avaliador = new Evaluation(entradasTreinamento);
            avaliador.crossValidateModel(classificador, entradasTreinamento, 15, new Random(1));

            arquivoTeste = new File("/users/user/desktop/formula1_pesquisa.arff");
            leitorTeste = new FileReader(arquivoTeste);
            entradasTeste = new Instances(leitorTeste);
            entradasTeste.setClassIndex(4);

            // Verificação da igualdade entre os cabeçalhos dos arquivos de treinamento e teste
            if (!entradasTreinamento.equalHeaders(entradasTeste)) {
                JOptionPane.showMessageDialog(null, "*** Arquivos ARFF de Treinamento e Teste são diferentes...",
                        "Informação", 1);
                //System.out.println("*** Arquivos ARFF de Treinamento e Teste são diferentes...");
                podeTestar = false;
            } else {
                JOptionPane.showMessageDialog(null, "*** Arquivos ARFF de Treinamento e Teste são iguais...",
                        "Informação", 1);
                //System.out.println("*** Arquivos ARFF de Treinamento e Teste são iguais...");
                podeTestar = true;
            }

        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, "Mensagem de Erro: " + erro.getMessage(),
                        "Informação de ERRO", 1);
            //System.out.println("Mensagem de Erro: " + erro.getMessage());
        }
        
         if (avaliador != null) {
            System.out.println("\n1. Apresentando os resultados da treinamento da classificação e validação ");
            System.out.println("=============================================================================");
            //JOptionPane.showMessageDialog(null, avaliador.toSummaryString(),
                   //     "Informação", 1);
             System.out.println(avaliador.toSummaryString());
            System.out.println("=============================================================================");
        }
         
         if (classificador != null && podeTestar) {
            System.out.println("\n2. Apresentando os resultados dos testes de classificação ");
            System.out.println("=============================================================================");
            try {
                for (int i = 0; i < entradasTeste.numInstances(); i++) {
                    // JOptionPane.showMessageDialog(null, avaliador.toSummaryString(),
                       // "Informação", 1);
                    System.out.println(classificador.classifyInstance(entradasTeste.instance(i)));
                }
            } catch (Exception erro) {
                System.out.println("Mensagem de Erro: " + erro.getMessage());

            }
            System.out.println("=============================================================================");
        }
    }//GEN-LAST:event_btnWEKAActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgrRadios;
    private javax.swing.JButton btnExport;
    private javax.swing.JButton btnWEKA;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox jcbComb;
    private javax.swing.JComboBox jcbParada;
    private javax.swing.JComboBox jcbPiloto;
    private javax.swing.JComboBox jcbPneu;
    private javax.swing.JComboBox jcbVoltas;
    private javax.swing.JRadioButton jrbComb;
    private javax.swing.JRadioButton jrbParada;
    private javax.swing.JRadioButton jrbPiloto;
    private javax.swing.JRadioButton jrbPneu;
    private javax.swing.JRadioButton jrbVoltas;
    private javax.swing.JLabel lblComb;
    private javax.swing.JLabel lblParada;
    private javax.swing.JLabel lblPesquisa;
    private javax.swing.JLabel lblPiloto;
    private javax.swing.JLabel lblPneu;
    private javax.swing.JLabel lblVoltas;
    // End of variables declaration//GEN-END:variables
}
